import time
from concurrent.futures import ThreadPoolExecutor, as_completed, wait, FIRST_COMPLETED


def get_html(times):
    time.sleep(times)
    print("get page {} success".format(times))
    return times


if __name__ == '__main__':
    executor = ThreadPoolExecutor(max_workers=1)
    # task1 = executor.submit(get_html, (3))
    # task2 = executor.submit(get_html, (2))
    # print(task1.done())
    # print(task2.cancel())
    # time.sleep(4)
    # print(task1.result())

    urls = [2, 3, 4, 5]
    all_task = [executor.submit(get_html, (url)) for url in urls]
    wait(all_task, return_when=FIRST_COMPLETED)
    print("main")
    # for future in as_completed(all_task):
    #     data = future.result()
    #     print("get {} page success".format(data))
    # for data in executor.map(get_html, urls):
    #     print("get {} page success".format(data))
